
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>协议 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="nerves.html" />
    
    
    <link rel="prev" href="gen-stage.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">第一部分：基础</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../basics/basics.html">
            
                <a href="../basics/basics.html">
            
                    
                    基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../basics/collections.html">
            
                <a href="../basics/collections.html">
            
                    
                    集合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../basics/enum.html">
            
                <a href="../basics/enum.html">
            
                    
                    Enum 模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../basics/pattern-matching.html">
            
                <a href="../basics/pattern-matching.html">
            
                    
                    模式匹配
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../basics/control-structures.html">
            
                <a href="../basics/control-structures.html">
            
                    
                    控制语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../basics/functions.html">
            
                <a href="../basics/functions.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../basics/pipe-operator.html">
            
                <a href="../basics/pipe-operator.html">
            
                    
                    管道操作符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../basics/modules.html">
            
                <a href="../basics/modules.html">
            
                    
                    模块(Module)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../basics/mix.html">
            
                <a href="../basics/mix.html">
            
                    
                    Mix
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../basics/sigils.html">
            
                <a href="../basics/sigils.html">
            
                    
                    魔符(Sigil)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../basics/documentation.html">
            
                <a href="../basics/documentation.html">
            
                    
                    文档模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../basics/testing.html">
            
                <a href="../basics/testing.html">
            
                    
                    测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../basics/comprehensions.html">
            
                <a href="../basics/comprehensions.html">
            
                    
                    推导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="../basics/strings.html">
            
                <a href="../basics/strings.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="../basics/date-time.html">
            
                <a href="../basics/date-time.html">
            
                    
                    日期和时间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="../basics/mix-tasks.html">
            
                <a href="../basics/mix-tasks.html">
            
                    
                    自定义Mix任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="../basics/iex-helpers.html">
            
                <a href="../basics/iex-helpers.html">
            
                    
                    IEx辅助函数
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第二部分：高级</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="erlang.html">
            
                <a href="erlang.html">
            
                    
                    和Erlang互操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="error-handling.html">
            
                <a href="error-handling.html">
            
                    
                    错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="escripts.html">
            
                <a href="escripts.html">
            
                    
                    可执行文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="concurrency.html">
            
                <a href="concurrency.html">
            
                    
                    并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="otp-concurrency.html">
            
                <a href="otp-concurrency.html">
            
                    
                    OTP并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="otp-supervisors.html">
            
                <a href="otp-supervisors.html">
            
                    
                    OTP Supervisors
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="otp-distribution.html">
            
                <a href="otp-distribution.html">
            
                    
                    OTP 分布式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="metaprogramming.html">
            
                <a href="metaprogramming.html">
            
                    
                    元编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="umbrella-projects.html">
            
                <a href="umbrella-projects.html">
            
                    
                    Umbrella Projects
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.10" data-path="typespec.html">
            
                <a href="typespec.html">
            
                    
                    Specifications and types
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.11" data-path="behaviours.html">
            
                <a href="behaviours.html">
            
                    
                    行为
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.12" data-path="gen-stage.html">
            
                <a href="gen-stage.html">
            
                    
                    GenStage
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="2.13" data-path="protocols.html">
            
                <a href="protocols.html">
            
                    
                    协议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.14" data-path="nerves.html">
            
                <a href="nerves.html">
            
                    
                    Nerves
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第三部分：ECTO</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../ecto/basics.html">
            
                <a href="../ecto/basics.html">
            
                    
                    Basics
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="../ecto/changesets.html">
            
                <a href="../ecto/changesets.html">
            
                    
                    Changesets
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="../ecto/associations.html">
            
                <a href="../ecto/associations.html">
            
                    
                    关联关系
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="../ecto/querying.html">
            
                <a href="../ecto/querying.html">
            
                    
                    查询
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第四部分：专题</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../specifics/plug.html">
            
                <a href="../specifics/plug.html">
            
                    
                    Plug
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="../specifics/eex.html">
            
                <a href="../specifics/eex.html">
            
                    
                    嵌入的 Elixir(EEx)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="../specifics/ets.html">
            
                <a href="../specifics/ets.html">
            
                    
                    Erlang 项式存储(ETS)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="../specifics/mnesia.html">
            
                <a href="../specifics/mnesia.html">
            
                    
                    Mnesia 数据库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="../specifics/debugging.html">
            
                <a href="../specifics/debugging.html">
            
                    
                    调试
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第五部分：程序库</li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="../libraries/guardian.html">
            
                <a href="../libraries/guardian.html">
            
                    
                    Guardian(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="../libraries/poolboy.html">
            
                <a href="../libraries/poolboy.html">
            
                    
                    Poolboy
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="../libraries/benchee.html">
            
                <a href="../libraries/benchee.html">
            
                    
                    Benchee
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="../libraries/bypass.html">
            
                <a href="../libraries/bypass.html">
            
                    
                    Bypass
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="../libraries/distillery.html">
            
                <a href="../libraries/distillery.html">
            
                    
                    Distillery(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="../libraries/stream-data.html">
            
                <a href="../libraries/stream-data.html">
            
                    
                    StreamData
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >协议</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <p>&#x6211;&#x4EEC;&#x5C06;&#x5728;&#x672C;&#x8BFE;&#x7A0B;&#x770B;&#x770B; Elixir &#x91CC;&#x9762;&#x7684;&#x534F;&#x8BAE;&#x5230;&#x5E95;&#x662F;&#x4EC0;&#x4E48;&#xFF0C;&#x4EE5;&#x53CA;&#x5982;&#x4F55;&#x4F7F;&#x7528;&#x3002;  </p>
<h2 id="&#x4EC0;&#x4E48;&#x662F;&#x534F;&#x8BAE;">&#x4EC0;&#x4E48;&#x662F;&#x534F;&#x8BAE;</h2>
<p>&#x534F;&#x8BAE;&#x5230;&#x5E95;&#x662F;&#x4EC0;&#x4E48;&#x5462;&#xFF1F;&#x534F;&#x8BAE;&#x662F; Elixir &#x5B9E;&#x73B0;&#x591A;&#x6001;&#x7684;&#x4E00;&#x79CD;&#x65B9;&#x5F0F;&#x3002;Erlang &#x7684;&#x5176;&#x4E2D;&#x4E00;&#x4E2A;&#x75DB;&#x70B9;&#xFF0C;&#x5C31;&#x662F;&#x4E3A;&#x73B0;&#x6709;&#x7684; API &#x6269;&#x5C55;&#x5230;&#x65B0;&#x5B9A;&#x4E49;&#x7684;&#x7C7B;&#x578B;&#x4E0A;&#x9762;&#x3002;&#x4E3A;&#x4E86;&#x5728; Elixir &#x91CC;&#x89E3;&#x51B3;&#x8FD9;&#x4E2A;&#x95EE;&#x9898;&#xFF0C;&#x51FD;&#x6570;&#x662F;&#x57FA;&#x4E8E;&#x6570;&#x503C;&#x7684;&#x7C7B;&#x578B;&#x6765;&#x52A8;&#x6001;&#x6D3E;&#x9063;&#x7684;&#x3002;Elixir &#x91CC;&#x9884;&#x5B9A;&#x4E49;&#x4E86;&#x597D;&#x4E00;&#x4E9B;&#x534F;&#x8BAE;&#xFF0C;&#x6BD4;&#x5982;&#x8BF4;&#xFF0C;<code>String.Chars</code>&#x3002;&#x8FD9;&#x4E2A;&#x534F;&#x8BAE;&#x5B9A;&#x4E49;&#x4E86;&#x6211;&#x4EEC;&#x4E4B;&#x524D;&#x770B;&#x8FC7;&#x548C;&#x7528;&#x8FC7;&#x7684; <code>to_string/1</code> &#x51FD;&#x6570;&#x3002;&#x8BA9;&#x6211;&#x4EEC;&#x7528;&#x4E00;&#x4E9B;&#x7B80;&#x5355;&#x7684;&#x4F8B;&#x5B50;&#x6765;&#x8FD1;&#x8DDD;&#x79BB;&#x770B;&#x770B; <code>to_string/1</code>&#x3002;  </p>
<pre><code class="lang-elixir">iex&gt; to_string(<span class="hljs-number">5</span>)
<span class="hljs-string">&quot;5&quot;</span>
iex&gt; to_string(<span class="hljs-number">12.4</span>)
<span class="hljs-string">&quot;12.4&quot;</span>
iex&gt; to_string(<span class="hljs-string">&quot;foo&quot;</span>)
<span class="hljs-string">&quot;foo&quot;</span>
</code></pre>
<p>&#x6B63;&#x5982;&#x4F60;&#x770B;&#x5230;&#x7684;&#xFF0C;&#x6211;&#x4EEC;&#x5728;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x7684;&#x65F6;&#x5019;&#x4F20;&#x5165;&#x597D;&#x51E0;&#x4E2A;&#x4E0D;&#x540C;&#x7C7B;&#x578B;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x800C;&#x4E14;&#x4E5F;&#x90FD;&#x80FD;&#x6B63;&#x5E38;&#x6267;&#x884C;&#x3002;&#x90A3;&#x5982;&#x679C;&#x6211;&#x4EEC;&#x5728;&#x8C03;&#x7528; <code>to_string/1</code> &#x7684;&#x65F6;&#x5019;&#x4F20;&#x5165;&#x5143;&#x7EC4;&#xFF08;&#x6216;&#x8005;&#x5176;&#x5B83;&#x6CA1;&#x6709;&#x5B9E;&#x73B0; <code>String.Chars</code> &#x534F;&#x8BAE;&#x7684;&#x7C7B;&#x578B;&#xFF09;&#x5462;&#xFF1F;&#x6765;&#x770B;&#x770B;&#x7ED3;&#x679C;&#xFF1A;  </p>
<pre><code class="lang-elixir">to_string({:foo})
** (Protocol.UndefinedError) protocol String.Chars not implemented for {:foo}
    (elixir) lib/string/chars.ex:3: String.Chars.impl_for!/1
    (elixir) lib/string/chars.ex:17: String.Chars.to_string/1
</code></pre>
<p>&#x7531;&#x4E8E;&#x6CA1;&#x6709;&#x4E3A;&#x5143;&#x7EC4;&#x63D0;&#x4F9B;&#x8FD9;&#x4E2A;&#x534F;&#x8BAE;&#x7684;&#x5B9E;&#x73B0;&#xFF0C;&#x4E00;&#x4E2A;&#x534F;&#x8BAE;&#x9519;&#x8BEF;&#x4EA7;&#x751F;&#x4E86;&#x3002;&#x4E0B;&#x4E00;&#x8282;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x4E3A;&#x5143;&#x7EC4;&#x63D0;&#x4F9B; <code>String.Chars</code> &#x534F;&#x8BAE;&#x7684;&#x5B9E;&#x73B0;&#x3002;  </p>
<h2 id="&#x5B9E;&#x73B0;&#x534F;&#x8BAE;">&#x5B9E;&#x73B0;&#x534F;&#x8BAE;</h2>
<p>&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x77E5;&#x9053;&#xFF0C;<code>to_string/1</code> &#x5E76;&#x6CA1;&#x6709;&#x4E3A;&#x5143;&#x7EC4;&#x63D0;&#x4F9B;&#x5B9E;&#x73B0;&#xFF0C;&#x6240;&#x4EE5;&#x8BA9;&#x6211;&#x4EEC;&#x6765;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x3002;&#x8981;&#x63D0;&#x4F9B;&#x67D0;&#x4E2A;&#x534F;&#x8BAE;&#x7684;&#x5B9E;&#x73B0;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x4F7F;&#x7528; <code>defimpl</code> &#x6765;&#x6307;&#x5B9A;&#x8FD9;&#x4E2A;&#x534F;&#x8BAE;&#xFF0C;&#x5E76;&#x7528; <code>:for</code> &#x9009;&#x9879;&#x6307;&#x660E;&#x7C7B;&#x578B;&#x3002;&#x8BA9;&#x6211;&#x4EEC;&#x770B;&#x770B;&#x4EE3;&#x7801;&#x5927;&#x6982;&#x662F;&#x4EC0;&#x4E48;&#x6837;&#x5B50;&#x7684;&#xFF1A;  </p>
<pre><code class="lang-elixir">defimpl String.Chars, <span class="hljs-symbol">for:</span> Tuple <span class="hljs-keyword">do</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">to_string</span></span>(tuple) <span class="hljs-keyword">do</span>
    interior =
      tuple
      |&gt; Tuple.to_list()
      |&gt; Enum.map(&amp;Kernel.to_string/<span class="hljs-number">1</span>)
      |&gt; Enum.join(<span class="hljs-string">&quot;, &quot;</span>)

    <span class="hljs-string">&quot;{<span class="hljs-subst">#{interior}</span>}&quot;</span>
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x5982;&#x679C;&#x628A;&#x4E0A;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x590D;&#x5236;&#x5230; IEx&#xFF0C;&#x6211;&#x4EEC;&#x73B0;&#x5728;&#x5C31;&#x53EF;&#x4EE5;&#x5728;&#x8C03;&#x7528; <code>to_string/1</code> &#x7684;&#x65F6;&#x5019;&#x4F20;&#x5165;&#x4E00;&#x4E2A;&#x5143;&#x7EC4;&#xFF0C;&#x800C;&#x4E14;&#x8FD8;&#x4E0D;&#x4F1A;&#x51FA;&#x9519;&#x4E86;&#xFF1A;  </p>
<pre><code class="lang-elixir">iex&gt; to_string({<span class="hljs-number">3.14</span>, <span class="hljs-string">&quot;apple&quot;</span>, <span class="hljs-symbol">:pie</span>})
<span class="hljs-string">&quot;{3.14, apple, pie}&quot;</span>
</code></pre>
<p>&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x77E5;&#x9053;&#x5982;&#x4F55;&#x5B9E;&#x73B0;&#x4E00;&#x4E2A;&#x63A5;&#x53E3;&#xFF0C;&#x4F46;&#x662F;&#x5982;&#x4F55;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x534F;&#x8BAE;&#x5462;&#xFF1F;&#x901A;&#x8FC7;&#x4E0B;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x4F1A;&#x5B9E;&#x73B0; <code>to_atom/1</code> &#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x3002;&#x6765;&#x770B;&#x770B;&#x5982;&#x4F55;&#x4F7F;&#x7528; <code>defprotocol</code> &#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x534F;&#x8BAE;&#xFF1A;  </p>
<pre><code class="lang-elixir">defprotocol AsAtom <span class="hljs-keyword">do</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">to_atom</span></span>(data)
<span class="hljs-keyword">end</span>

defimpl AsAtom, <span class="hljs-symbol">for:</span> Atom <span class="hljs-keyword">do</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">to_atom</span></span>(atom), <span class="hljs-symbol">do:</span> atom
<span class="hljs-keyword">end</span>

defimpl AsAtom, <span class="hljs-symbol">for:</span> BitString <span class="hljs-keyword">do</span>
  defdelegate to_atom(string), <span class="hljs-symbol">to:</span> String
<span class="hljs-keyword">end</span>

defimpl AsAtom, <span class="hljs-symbol">for:</span> List <span class="hljs-keyword">do</span>
  defdelegate to_atom(list), <span class="hljs-symbol">to:</span> List
<span class="hljs-keyword">end</span>

defimpl AsAtom, <span class="hljs-symbol">for:</span> Map <span class="hljs-keyword">do</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">to_atom</span></span>(map), <span class="hljs-symbol">do:</span> List.first(Map.keys(map))
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x8FD9;&#x91CC;&#xFF0C;&#x6211;&#x4EEC;&#x5B9A;&#x4E49;&#x4E86;&#x81EA;&#x5DF1;&#x7684;&#x534F;&#x8BAE;&#xFF0C;&#x4EE5;&#x53CA;&#x5B83;&#x63D0;&#x4F9B;&#x7684;&#x51FD;&#x6570; <code>to_atom/1</code>&#xFF0C;&#x8FD8;&#x6709;&#x67D0;&#x4E00;&#x4E9B;&#x7C7B;&#x578B;&#x7684;&#x5B9E;&#x73B0;&#x65B9;&#x5F0F;&#x3002;&#x65E2;&#x7136;&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x62E5;&#x6709;&#x4E86;&#x81EA;&#x5DF1;&#x7684;&#x534F;&#x8BAE;&#xFF0C;&#x90A3;&#x5C31;&#x653E;&#x5230; IEx &#x91CC;&#x9762;&#x6765;&#x4F7F;&#x7528;&#x4E00;&#x4E0B;&#xFF1A;  </p>
<pre><code class="lang-elixir">iex&gt; import AsAtom
AsAtom
iex&gt; to_atom(<span class="hljs-string">&quot;string&quot;</span>)
<span class="hljs-symbol">:string</span>
iex&gt; to_atom(<span class="hljs-symbol">:an_atom</span>)
<span class="hljs-symbol">:an_atom</span>
iex&gt; to_atom([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>])
<span class="hljs-symbol">:<span class="hljs-string">&quot;\x01\x02&quot;</span></span>
iex&gt; to_atom(%{<span class="hljs-symbol">foo:</span> <span class="hljs-string">&quot;bar&quot;</span>})
<span class="hljs-symbol">:foo</span>
</code></pre>
<p>&#x8981;&#x6CE8;&#x610F;&#x7684;&#x662F;&#xFF0C;&#x867D;&#x7136;&#x7ED3;&#x6784;&#x4F53;&#x7684;&#x672C;&#x8D28;&#x662F;&#x6620;&#x5C04;&#xFF08;Map&#xFF09;&#xFF0C;&#x4F46;&#x662F;&#x5B83;&#x4EEC;&#x5E76;&#x4E0D;&#x548C;&#x6620;&#x5C04;&#x5171;&#x4EAB;&#x534F;&#x8BAE;&#x7684;&#x5B9E;&#x73B0;&#x65B9;&#x5F0F;&#x3002;&#x5B83;&#x4EEC;&#x4E0D;&#x53EF;&#x904D;&#x5386;&#xFF0C;&#x6240;&#x4EE5;&#x4E5F;&#x4E0D;&#x80FD;&#x4EE5;&#x8FD9;&#x79CD;&#x65B9;&#x5F0F;&#x8BBF;&#x95EE;&#x3002;  </p>
<p>&#x5982;&#x4F60;&#x6240;&#x89C1;&#xFF0C;&#x534F;&#x8BAE;&#x662F;&#x4E00;&#x79CD;&#x5F3A;&#x5927;&#x7684;&#x5B9E;&#x73B0;&#x591A;&#x6001;&#x7684;&#x624B;&#x6BB5;&#x3002;  </p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="gen-stage.html" class="navigation navigation-prev " aria-label="Previous page: GenStage">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="nerves.html" class="navigation navigation-next " aria-label="Next page: Nerves">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"version":"1.0.1","title":"协议","level":"2.13","depth":1,"next":{"title":"Nerves","level":"2.14","depth":1,"path":"lessons/advanced/nerves.md","ref":"lessons/advanced/nerves.md","articles":[]},"previous":{"title":"GenStage","level":"2.12","depth":1,"path":"lessons/advanced/gen-stage.md","ref":"lessons/advanced/gen-stage.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"lessons/advanced/protocols.md","mtime":"2019-11-05T02:25:51.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-11-05T03:01:22.004Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

